package com.ethank.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.ethank.common.core.domain.AjaxResult;
import com.ethank.enums.BillEnum;
import com.ethank.service.WxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Component
public class BillUtil {

    @Autowired
    WxService wxService;

    private static final Logger logger = LoggerFactory.getLogger(BillUtil.class);


    /**
     * 混贴发票识别
     *
     * @param file 文件
     * @return 返回数据
     * @throws Exception 异常
     */
    public AjaxResult HTBill(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);


        RecognizeMixedInvoicesRequest recognizeMixedInvoicesRequest = new RecognizeMixedInvoicesRequest();
        RuntimeOptions runtime = new RuntimeOptions();

        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            recognizeMixedInvoicesRequest.setBody(inputStream);
            // 复制代码运行请自行打印 API 的返回值
            RecognizeMixedInvoicesResponse recognizeMixedInvoicesResponse =
                    client.recognizeMixedInvoicesWithOptions(recognizeMixedInvoicesRequest, runtime);
            String data = recognizeMixedInvoicesResponse.getBody().getData();
            JSONObject jo = JSON.parseObject(data);
            JSONArray subMsgs = jo.getJSONArray("subMsgs");
            ArrayList<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            System.out.println("所有识别后的数据 -----> {}" + subMsgs);
            // 遍历每一个
            for (int i = 0; i < subMsgs.size(); i++) {
                Object e = subMsgs.get(i);
                System.out.println("每一条数据 -----> {}" + e);
                HashMap<String, Object> resultMap = new HashMap<>();
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(e));
                JSONObject result1 = jsonObject.getJSONObject("result");
                JSONObject data1 = result1.getJSONObject("data");
                // 注释转相应数据库实体
              /*  if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                    String tp = jsonObject.get("type").toString();
                    Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                    if(null == billTypeInteger){
                        continue;
                    }

                    if (result1.get("ftype") != null) {
                        billType(billTypeInteger, data1, result1.get("ftype").toString());
                    } else {
                        billType(billTypeInteger, data1, "");
                    }
                    data1.put("billType", billTypeInteger);
                }*/
                // 装载数据
                resultMap.put("ftype", result1.get("ftype"));
                resultMap.put("index", jsonObject.get("index"));
                resultMap.put("op", jsonObject.get("op"));
                resultMap.put("type", jsonObject.get("type"));
                resultMap.put("data", data1);
                maps.add(resultMap);
                System.out.println("------------");
            }

            return AjaxResult.success(maps);
        } catch (TeaException error) {
            logger.error("混贴发票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("混贴发票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("混贴发票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error("图像内容错误或格式不支持");
    }


    /**
     * @Description: 酒店流水识别
     * @Param: [file, accessKeyId, accessKeySecret]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Author: tf
     * @Date: 2024-02-26
     */
    public AjaxResult hotelConsume(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);

            RecognizeHotelConsumeRequest recognizeHotelConsumeRequest =
                    new RecognizeHotelConsumeRequest().setBody(inputStream);
            RecognizeHotelConsumeResponse recognizeHotelConsumeResponse =
                    client.recognizeHotelConsume(recognizeHotelConsumeRequest);
            String data = recognizeHotelConsumeResponse.getBody().getData();
            JSONObject jsonObject = JSON.parseObject(data);
            System.out.println("所有识别后的数据 -----> {}" + jsonObject);
            JSONObject resultData = jsonObject.getJSONObject("data");
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("酒店流水识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("酒店流水识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("酒店流水识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * @Description: 支付详情识别
     * @Param: [file, accessKeyId, accessKeySecret]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Author: tf
     * @Date: 2024-02-26
     */
    public AjaxResult paymentRecord(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();

        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizePaymentRecordRequest recognizePaymentRecordRequest =
                    new RecognizePaymentRecordRequest().setBody(inputStream);
            RecognizePaymentRecordResponse recognizePaymentRecordResponse =
                    client.recognizePaymentRecordWithOptions(recognizePaymentRecordRequest, runtime);
            String data = recognizePaymentRecordResponse.getBody().getData();
            logger.info("BillUtil=>paymentRecord=>支付详情识别=>{}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("支付详情识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("支付详情识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("支付详情识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 增值税发票识别
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 识别增值税发票
     */
    public AjaxResult recognizeInvoice(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeInvoiceRequest recognizeInvoiceRequest = new RecognizeInvoiceRequest().setBody(inputStream);
            RecognizeInvoiceResponse recognizeInvoiceResponse =
                    client.recognizeInvoiceWithOptions(recognizeInvoiceRequest, runtime);
            String data = recognizeInvoiceResponse.getBody().getData();
            logger.info("BillUtil => recognizeInvoice => 识别增值税发票 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");


            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }


            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("增值税发票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("增值税发票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("增值税发票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 通用机打发票识别
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 通用机打发票识别
     */
    public AjaxResult commonPrintedInvoice(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeCommonPrintedInvoiceRequest recognizeCommonPrintedInvoiceRequest =
                    new RecognizeCommonPrintedInvoiceRequest().setBody(inputStream);
            RecognizeCommonPrintedInvoiceResponse recognizeCommonPrintedInvoiceResponse =
                    client.recognizeCommonPrintedInvoiceWithOptions(recognizeCommonPrintedInvoiceRequest, runtime);
            String data = recognizeCommonPrintedInvoiceResponse.getBody().getData();
            logger.info("BillUtil => commonPrintedInvoice => 通用机打发票识别 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("commonPrintedInvoice ==> 通用机打发票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("commonPrintedInvoice ==> 通用机打发票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("commonPrintedInvoice ==> 通用机打发票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 出租车发票识别
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 出租车发票识别
     */
    public AjaxResult taxiInvoice(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeTaxiInvoiceRequest recognizeTaxiInvoiceRequest =
                    new RecognizeTaxiInvoiceRequest().setBody(inputStream);
            RecognizeTaxiInvoiceResponse recognizeTaxiInvoiceResponse =
                    client.recognizeTaxiInvoiceWithOptions(recognizeTaxiInvoiceRequest, runtime);
            String data = recognizeTaxiInvoiceResponse.getBody().getData();
            logger.info("BillUtil => taxiInvoice => 出租车发票识别 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("taxiInvoice ==> 出租车发票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("taxiInvoice ==> 出租车发票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("taxiInvoice ==> 出租车发票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 火车票识别
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 火车票识别
     */
    public AjaxResult trainInvoice(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeTrainInvoiceRequest recognizeTrainInvoiceRequest =
                    new RecognizeTrainInvoiceRequest().setBody(inputStream);
            RecognizeTrainInvoiceResponse recognizeTrainInvoiceResponse =
                    client.recognizeTrainInvoiceWithOptions(recognizeTrainInvoiceRequest, runtime);
            String data = recognizeTrainInvoiceResponse.getBody().getData();
            logger.info("BillUtil => trainInvoice => 火车票识别 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("trainInvoice ==> 火车票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("trainInvoice ==> 火车票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("trainInvoice ==> 火车票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 过路过桥发票识别
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 过路过桥发票识别
     */
    public AjaxResult tollInvoice(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeTollInvoiceRequest recognizeTrainInvoiceRequest =
                    new RecognizeTollInvoiceRequest().setBody(inputStream);
            RecognizeTollInvoiceResponse recognizeTollInvoiceResponse =
                    client.recognizeTollInvoiceWithOptions(recognizeTrainInvoiceRequest, runtime);
            String data = recognizeTollInvoiceResponse.getBody().getData();
            logger.info("BillUtil => tollInvoice => 过路过桥发票识别 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("tollInvoice ==> 过路过桥发票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("tollInvoice ==> 过路过桥发票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("tollInvoice ==> 过路过桥发票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 客运车船票识别
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 客运车船票识别
     */
    public AjaxResult busShipTicket(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeBusShipTicketRequest recognizeBusShipTicketRequest =
                    new RecognizeBusShipTicketRequest().setBody(inputStream);
            RecognizeBusShipTicketResponse recognizeBusShipTicketResponse =
                    client.recognizeBusShipTicketWithOptions(recognizeBusShipTicketRequest, runtime);
            String data = recognizeBusShipTicketResponse.getBody().getData();
            logger.info("BillUtil => busShipTicket => 客运车船票识别 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("busShipTicket ==> 客运车船票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("busShipTicket ==> 客运车船票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("busShipTicket ==> 客运车船票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 机票行程单识别
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 机票行程单识别
     */
    public AjaxResult airItinerary(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeAirItineraryRequest recognizeAirItineraryRequest =
                    new RecognizeAirItineraryRequest().setBody(inputStream);
            RecognizeAirItineraryResponse recognizeAirItineraryResponse =
                    client.recognizeAirItineraryWithOptions(recognizeAirItineraryRequest, runtime);
            String data = recognizeAirItineraryResponse.getBody().getData();
            logger.info("BillUtil => airItinerary => 机票行程单识别 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("airItinerary ==> 机票行程单识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("airItinerary ==> 机票行程单识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("airItinerary ==> 机票行程单识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 增值税发票卷票
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 增值税发票卷票
     */
    public AjaxResult rollTicket(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeRollTicketRequest recognizeRollTicketRequest =
                    new RecognizeRollTicketRequest().setBody(inputStream);
            RecognizeRollTicketResponse recognizeRollTicketResponse =
                    client.recognizeRollTicketWithOptions(recognizeRollTicketRequest, runtime);
            String data = recognizeRollTicketResponse.getBody().getData();
            logger.info("BillUtil => rollTicket => 增值税发票卷票 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("rollTicket ==> 增值税发票卷票 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("rollTicket ==> 增值税发票卷票 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("rollTicket ==> 增值税发票卷票 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 网约车行程单
     *
     * @param file            文件
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return 返回数据
     * @Description: 网约车行程单
     */
    public AjaxResult rideHailingItinerary(InputStream inputStream, String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        Client client = new Client(config);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeRideHailingItineraryRequest recognizeRideHailingItineraryRequest =
                    new RecognizeRideHailingItineraryRequest().setBody(inputStream);
            RecognizeRideHailingItineraryResponse recognizeRideHailingItineraryResponse =
                    client.recognizeRideHailingItineraryWithOptions(recognizeRideHailingItineraryRequest, runtime);
            String data = recognizeRideHailingItineraryResponse.getBody().getData();
            logger.info("BillUtil => rideHailingItinerary => 网约车行程单 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("rideHailingItinerary ==> 网约车行程单 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("rideHailingItinerary ==> 网约车行程单 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("rideHailingItinerary ==> 网约车行程单 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }


    /**
     * 定额发票识别
     *
     * @param inputStream     文件流
     * @param accessKeyId     id
     * @param accessKeySecret key
     * @return 返回
     */
    public AjaxResult quotaInvoice(InputStream inputStream, String accessKeyId, String accessKeySecret) {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            // byte[] byteArr = file.getBytes();
            // InputStream inputStream = new ByteArrayInputStream(byteArr);
            RecognizeQuotaInvoiceRequest recognizeRideHailingItineraryRequest =
                    new RecognizeQuotaInvoiceRequest().setBody(inputStream);
            RecognizeQuotaInvoiceResponse recognizeQuotaInvoiceResponse = client.recognizeQuotaInvoiceWithOptions(recognizeRideHailingItineraryRequest, runtime);
            String data = recognizeQuotaInvoiceResponse.getBody().getData();
            logger.info("BillUtil => quotaInvoice => 定额发票识别 => {}", data);
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject resultData = jsonObject.getJSONObject("data");
            // 数据操作
            if (jsonObject.get("type") != null && !"".equals(jsonObject.get("type"))) {
                String tp = jsonObject.get("type").toString();
                Integer billTypeInteger = BillEnum.getTypeByBillTypeName(tp);
                if (jsonObject.get("ftype") != null) {
                    billType(billTypeInteger, resultData, jsonObject.get("ftype").toString());
                } else {
                    billType(billTypeInteger, resultData, "");
                }
                resultData.put("billType", billTypeInteger);
            }
            return AjaxResult.success(resultData);
        } catch (TeaException error) {
            logger.error("quotaInvoice ==> 定额发票识别 ==> 识别失败 ==> statusCode ==> {}", error.getStatusCode());
            logger.error("quotaInvoice ==> 定额发票识别 ==> 识别失败 ==> code ==> {}", error.getCode());
            logger.error("quotaInvoice ==> 定额发票识别 ==> 识别失败 ==> message ==> {}", error.getMessage());
            // 错误 message
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            logger.error(error.getMessage());
            // 诊断地址
            logger.error(error.getData().get("Recommend").toString());
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return AjaxResult.error();
    }




    /**
     * @param typeInt  票据类型
     * @param dataInfo 三方接口获取的数据
     * @param ftype    原件/复印件
     * @desc 每一个识别接口获取数据后调用此方法，处理数据，并传入库中
     */
    // @Transactional(rollbackFor = Exception.class)
    public void billType(Integer typeInt, JSONObject dataInfo, String ftype) {
        BillDataUtil billDataUtil = new BillDataUtil();
        if (dataInfo.containsKey("id")) {
            BillDataUtil.billDataUtil.fnaInvoiceLedgerMapper.deleteFnaInvoiceLedgerDtl(dataInfo.getInteger("id"));
            BillDataUtil.billDataUtil.fnaInvoiceLedgerMapper.deleteFnaInvoiceLedgerDetail(dataInfo.getInteger("id"));
        }
        switch (typeInt) {
            case 1:
                BillDataUtil.billDataUtil.recognizeInvoiceData(dataInfo, ftype, typeInt);
                break;
            case 3:

                billDataUtil.commonPrintedInvoice(dataInfo, ftype, typeInt);
                break;
            case 5:
                billDataUtil.quotaInvoice(dataInfo, ftype, typeInt);
                break;
            case 7:
                billDataUtil.taxiInvoice(dataInfo, ftype, typeInt);
                break;
            case 8:

                billDataUtil.trainInvoice(dataInfo, ftype, typeInt);
                break;
            case 9:

                billDataUtil.tollInvoice(dataInfo, ftype, typeInt);
                break;
            case 10:

                billDataUtil.busShipTicket(dataInfo, ftype, typeInt);
                break;
            case 14:

                billDataUtil.airItinerary(dataInfo, ftype, typeInt);
                break;
            case 16:
                billDataUtil.rollTicket(dataInfo, ftype, typeInt);
                break;
            case 21:

                billDataUtil.rideHailingItinerary(dataInfo, ftype, typeInt);
                break;
            default:
                System.out.println("系统错误");

        }
    }
}
